package com.tentcoo.zhaogonggu.module.homepage.b;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tentcoo.zhaogonggu.R;
import com.tentcoo.zhaogonggu.common.CaseInfo;
import java.util.List;

/**
 * Created by Gunter on 2015/4/19.
 * @author 林桂填
 */
public class CaseListAdapter extends BaseAdapter{
    private LayoutInflater mInflater;
    private List<CaseInfo> caseList;


    public CaseListAdapter(Context context,List<CaseInfo> caseList) {
        mInflater = LayoutInflater.from(context);
        this.caseList = caseList;
    }

    @Override
    public int getCount() {
        return caseList.size();
    }

    @Override
    public Object getItem(int position) {
        return caseList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.assessor_case_item,null);
            viewHolder.img_caseState = (ImageView) convertView.findViewById(R.id.img_caseState);
            viewHolder.img_caseView = (ImageView) convertView.findViewById(R.id.img_caseView);
            viewHolder.img_isNew = (ImageView) convertView.findViewById(R.id.img_isNew);
            viewHolder.tv_caseDate = (TextView) convertView.findViewById(R.id.tv_caseDate);
            viewHolder.tv_caseState = (TextView) convertView.findViewById(R.id.tv_caseState);
            viewHolder.tv_clientName = (TextView) convertView.findViewById(R.id.tv_clientName);
            viewHolder.tv_insuType = (TextView) convertView.findViewById(R.id.tv_insuType);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        CaseInfo caseInfo = caseList.get(position);
        CaseInfo.State caseState = caseInfo.getCaseState();
        viewHolder.img_caseView.setImageBitmap(caseInfo.getCaseImg());
        viewHolder.tv_insuType.setText(caseInfo.getInsuType());
        viewHolder.tv_clientName.setText(caseInfo.getClient());
        setState(caseState, viewHolder);
        if(caseInfo.isNew()==true){
            viewHolder.img_isNew.setVisibility(View.VISIBLE);
        }else {
            viewHolder.img_isNew.setVisibility(View.INVISIBLE);
        }
        viewHolder.tv_caseDate.setText(caseInfo.getDate());
        return convertView;
    }
    private void setState(CaseInfo.State caseState, ViewHolder viewHolder){
        if(caseState.equals(CaseInfo.State.refused)){
            viewHolder.img_caseState.setImageBitmap(BitmapFactory.decodeResource(mInflater.getContext().getResources(),R.drawable.refuse));
            viewHolder.tv_caseState.setText(R.string.case_state_refused);
            viewHolder.tv_caseState.setTextColor(mInflater.getContext().getResources().getColor(R.color.case_state_refused));
        }else if (caseState.equals(CaseInfo.State.accepted)){
            viewHolder.img_caseState.setImageBitmap(BitmapFactory.decodeResource(mInflater.getContext().getResources(),R.drawable.accept));
            viewHolder.tv_caseState.setText(R.string.case_state_accepted);
            viewHolder.tv_caseState.setTextColor(mInflater.getContext().getResources().getColor(R.color.case_state_accepted));
        }else if (caseState.equals(CaseInfo.State.evaluated)){
            viewHolder.img_caseState.setImageBitmap(BitmapFactory.decodeResource(mInflater.getContext().getResources(),R.drawable.evaluate));
            viewHolder.tv_caseState.setText(R.string.case_state_evaluated);
            viewHolder.tv_caseState.setTextColor(mInflater.getContext().getResources().getColor(R.color.case_state_evaluated));
        }else{
            viewHolder.img_caseState.setImageBitmap(BitmapFactory.decodeResource(mInflater.getContext().getResources(),R.drawable.check));
            viewHolder.tv_caseState.setText(R.string.case_state_unscaned);
            viewHolder.tv_caseState.setTextColor(mInflater.getContext().getResources().getColor(R.color.case_state_unscaned));
        }
    }
    private class ViewHolder{
        public ImageView img_caseState;
        public ImageView img_caseView;
        public ImageView img_isNew;
        public TextView tv_clientName;
        public TextView tv_insuType;
        public TextView tv_caseDate;
        public TextView tv_caseState;
    }
}
